package response

import (
	"app/internal/pkg/ecode"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Response(ctx *gin.Context, httpStatus int, code int, msg string, data any) {
	ctx.JSON(httpStatus, gin.H{
		"code": code,
		"msg":  msg,
		"data": data,
	})
}

func Success(ctx *gin.Context, msg string, data any) {
	Response(ctx, http.StatusOK, ecode.OK.Code, msg, data)
}

func Fail(ctx *gin.Context, err error) {
	switch err := err.(type) {
	case ecode.Ecode:
		Response(ctx, http.StatusBadRequest, err.Code, err.Msg, nil)
	default:
		Response(ctx, http.StatusBadRequest, ecode.FAIL.Code, err.Error(), nil)
	}
}

func Error(ctx *gin.Context, err error) {
	switch err := err.(type) {
	case ecode.Ecode:
		Response(ctx, http.StatusInternalServerError, err.Code, err.Msg, nil)
	default:
		Response(ctx, http.StatusInternalServerError, ecode.ERROR.Code, err.Error(), nil)
	}
}
